package com.yd.app.mapper;

import com.yd.app.mapper.provider.UserProvider;
import com.yd.domain.searchParam.UserSearchParam;
import com.yd.domain.sys.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by zg
 */
@Repository
public interface UserMapper {


    //根据id查询用户信息
    @Select("select * from sys_user where id=#{userId}")
    User getUserById(@Param("userId") String userId);
    //根据用户名查询用户数量
    @Select("select count(0) from sys_user where userName =#{userName} ")
    int getUserByUserName(@Param("userName") String userName);
    @Select("select * from sys_user where userName =#{userName} ")
    User getUserInfoByUserName(@Param("userName") String userName);
    //注册用户信息
    @Insert("insert into sys_user (id,userName,password,status,type,realName,phone,registCode,checkStatus,checkDes,agreementStatus,registTime,idCard,xydm,yyzz,companyName,agentId,phonePlace) " +
            "values (#{user.id},#{user.userName},#{user.password},#{user.status},#{user.type},#{user.realName},#{user.phone},#{user.registCode}," +
            "#{user.checkStatus},#{user.checkDes},#{user.agreementStatus},#{user.registTime},#{user.idCard},#{user.xydm},#{user.yyzz},#{user.companyName},#{user.agentCompany},#{user.phonePlace}) ")
    void userRegister(@Param("user") User user);
    //通过id删除用户信息
    @Delete("delete from sys_user where id=#{userId} ")
    void deleteUserById(@Param("userId") String userId);
    //修改用户信息
    @Update("update sys_user set userName=#{user.userName},password=#{user.password},status=#{user.status},type=#{user.type},realName=#{user.realName},phone=#{user.phone},checkStatus=#{user.checkStatus},checkDes=#{user.checkDes}," +
            "agreementStatus=#{user.agreementStatus},registTime=#{user.registTime},idCard=#{user.idCard},xydm=#{user.xydm},yyzz=#{user.yyzz},companyName=#{user.companyName},phonePlace=#{user.phonePlace} " +
            " where registCode=#{user.registCode} ")
    void userUpdate(@Param("user") User user);

    //查询用户数据
    @SelectProvider(method = "getUserList",type= UserProvider.class)
    List<User> getUserList(@Param("model")UserSearchParam param,@Param("startRow") int startRow, @Param("pageSize") int pageSize);
    //查询用户数量
    @SelectProvider(method = "getUserListCount",type= UserProvider.class)
    int getUserListCount(@Param("model")UserSearchParam param);
    //根据注册码查询
    @Select("select * from sys_user where userName =#{user.userName} and registCode=#{user.registCode} ")
    User getUserByReg(@Param("user")User user);

    @Select("select permCode from sys_role_perm where roleId in (select code from sys_role where code in (SELECT roleCode from sys_user_role where userId=#{userId}) )")
    List<String> getUserPermCode(@Param("userId") String userId);

    @Select("select * from  sys_user where id!=#{userId} ")
    List<User> getUserListAll(@Param("userId") String userId);

    //修改密码
    @Select("update sys_user set password=#{password} where id=#{id} ")
    void updatePwd(@Param("id") String id,@Param("password") String password);

    //审核用户
    @Update("update  sys_user set  checkStatus=#{checkStatus} where id=#{userId} ")
    boolean userCheckByuserId(@Param("userId") String userId,@Param("checkStatus") String checkStatus);

    @Select("select count(1) from sys_user where agentId=#{agentId} ")
    int  getUserByAgentId(@Param("agentId") String agentId);

    @Select("select b.title from sys_user_role a, sys_role b where a.roleCode=b.code and a.userId=#{userId}")
    List<String> getRole(@Param("userId") String userId);

    @Select("select * from sys_user where userName=#{username} and phone=#{phone}")
    List<User> justPhoneAndName(@Param("username") String username,@Param("phone") String phone);

    @Update("update sys_user set password=#{password} where id=#{id}")
    int changePwd(@Param("id") String id,@Param("password") String password);

}

